package com.ruoyi.member.service.impl;

import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.member.domain.BmInordergoodslist;
import com.ruoyi.member.domain.BmInorderlist;
import com.ruoyi.member.domain.BmWarehouse;
import com.ruoyi.member.domain.MyMap;
import com.ruoyi.member.mapper.BmInordergoodslistMapper;
import com.ruoyi.member.mapper.BmInorderlistMapper;
import com.ruoyi.member.mapper.SellerMyOrderMapper;
import com.ruoyi.member.service.IBmInordergoodslistService;
import com.ruoyi.member.service.IBmInorderlistService;
import com.ruoyi.member.service.ISellerMyOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author huang
 * @Date 2023/8/11 19:50
 * @PackageName: com.ruoyi.member.service.impl
 * @ClassName: SellerMyOrderServiceImpl
 * @Description:
 * @Version 1.0
 */

@Service("myService")
public class SellerMyOrderServiceImpl implements ISellerMyOrderService {

    @Autowired
    private IBmInorderlistService bmInorderlistService;
    @Autowired
    private IBmInordergoodslistService bmInordergoodslistService;
    @Autowired
    private RedisService redisService;

    @Autowired
    private SellerMyOrderMapper sellerMyOrderMapper;


    /**
     * @Description: 取消订单
     * @Param:
     * @return:
     * @Author: huang
     * @Date: 2023/8/12
     */
    @Override
    public boolean cancelOrder(String orderCode) {
        BmInorderlist bmInorderlist = bmInorderlistService.selectAll(orderCode);
        if (!(bmInorderlist.getOrderStep() < 3)) {
            return false;
        }
        List<BmInordergoodslist> bmInordergoodslists = bmInordergoodslistService.selectByOrderCode(orderCode);
        for (int i = 0; i < bmInordergoodslists.size(); i++) {
            bmInordergoodslistService.addStock(bmInordergoodslists.get(i));
        }
        bmInorderlistService.deleteBmInorderlistByOrderCode(orderCode);
        bmInordergoodslistService.deleteByOrderCode(orderCode);
        return true;
    }




    @Override
    public void dispatch(BmInorderlist bmInorderlist) {
        String warehouseCode = bmInorderlist.getWarehouseCode();
        BmWarehouse userStatus = sellerMyOrderMapper.selectByWarehouseCode(warehouseCode);
        List<Integer> warehouseUserList = sellerMyOrderMapper.getWarehouseUserList(warehouseCode);
        List<Integer> wuliuUserList = sellerMyOrderMapper.getwuliuUserList(warehouseCode);
        Integer warehouseUserStatus = userStatus.getWarehouseUserStatus();
        if (warehouseUserStatus == null) {
            warehouseUserStatus = 0;
        }
        Integer wuliuUserStatus = userStatus.getWuliuUserStatus();
        if (wuliuUserStatus == null) {
            wuliuUserStatus = 0;
        }
        Integer warehouserId = null;
        Integer logisticsId = null;
        if (warehouseUserStatus == 0) {
            Integer warehouseShunxu = Integer.valueOf(redisService.getCacheObject("warehouseShunxu:" + warehouseCode).toString());
            if (warehouseShunxu != (warehouseUserList.size() - 1)) {
                warehouserId = warehouseUserList.get(warehouseShunxu + 1);
                redisService.setCacheObject("warehouseShunxu:" + warehouseCode,warehouseShunxu + 1);
            } else {
                redisService.setCacheObject("warehouseShunxu:" + warehouseCode,0);
                warehouserId = warehouseUserList.get(0);
            }
        } else {
            List<Object> warehouseAnjian = redisService.getCacheList("warehouseAnjian:" + warehouseCode);
            List<BmInordergoodslist> bmInordergoodslists = bmInordergoodslistService.selectByOrderCode(bmInorderlist.getOrderCode());
            long shuliang = 0;
            for (int i = 0; i < bmInordergoodslists.size(); i++) {
                shuliang = shuliang + bmInordergoodslists.get(i).getBuyNum();
            }
            Integer index = null;
            Integer min = Integer.valueOf(warehouseAnjian.get(0).toString());
            for (int i = 0; i < warehouseAnjian.size(); i++) {
                Integer shuliang1 = Integer.valueOf(warehouseAnjian.get(i).toString());
                if (min >= shuliang1) {
                    min = shuliang1;
                    index = i;
                }
            }
            redisService.updateList("warehouseAnjian:" + warehouseCode,index.longValue(),min + shuliang);
            warehouserId = warehouseUserList.get(index);
        }
        if (wuliuUserStatus == 0) {
            Integer wuliuShunxu = Integer.valueOf(redisService.getCacheObject("wuliuShunxu:" + warehouseCode).toString());
            if (wuliuShunxu != (wuliuUserList.size() - 1)) {
                logisticsId = wuliuUserList.get(wuliuShunxu + 1);
                redisService.setCacheObject("wuliuShunxu:" + warehouseCode, wuliuShunxu + 1);
            } else {
                redisService.setCacheObject("wuliuShunxu:" + warehouseCode,0);
                logisticsId = wuliuUserList.get(0);
            }
        } else {
            List<Object> wuliuAnjian = redisService.getCacheList("wuliuAnjian:" + warehouseCode);
            List<BmInordergoodslist> bmInordergoodslists = bmInordergoodslistService.selectByOrderCode(bmInorderlist.getOrderCode());
            long shuliang = 0;
            for (int i = 0; i < bmInordergoodslists.size(); i++) {
                shuliang = shuliang + bmInordergoodslists.get(i).getBuyNum();
            }
            Integer index = null;
            Integer min = Integer.valueOf(wuliuAnjian.get(0).toString());
            for (int i = 0; i < wuliuAnjian.size(); i++) {
                Integer shuliang1 = Integer.valueOf(wuliuAnjian.get(i).toString());
                if (min >= shuliang1) {
                    min = shuliang1;
                    index = i;
                }
            }
            redisService.updateList("wuliuAnjian:" + warehouseCode,index.longValue(),min + shuliang);
            logisticsId = wuliuUserList.get(index);
        }
        BmInorderlist bmInorderlist1 = new BmInorderlist();
        bmInorderlist1.setWarehouserId(Long.valueOf(warehouserId));
        bmInorderlist1.setLogisticsId(Long.valueOf(logisticsId));
        bmInorderlist1.setWarehouseCode(warehouseCode);
        bmInorderlist1.setOrderCode(bmInorderlist.getOrderCode());
        sellerMyOrderMapper.dispatch(bmInorderlist1);
    }

    /**
     * @Description: 查询订单列表
     * @Param:
     * @return:
     * @Author: huang
     * @Date: 2023/8/14
     */
    @Override
    public List<BmInorderlist> getOrderList(int orderStepFlag) {
        List<BmInorderlist> orderList = null;
        switch (orderStepFlag) {
            case 0 :
                orderList = sellerMyOrderMapper.getOrderList(1);
                break;
            case 1 :
                orderList = sellerMyOrderMapper.getOrderList1(1);
                break;
            case 2 :
                orderList = sellerMyOrderMapper.getOrderList2(1);
                break;
            case 3 :
                orderList = sellerMyOrderMapper.getOrderList3(1);
                break;
            case 4 :
                orderList = sellerMyOrderMapper.getOrderList4(1);
                break;
        }
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        for (int i = 0; i < orderList.size(); i++) {
            String orderCode = orderList.get(i).getOrderCode();
            try {
                Date parse = format.parse(format.format(orderList.get(i).getCreateTime()));
                orderList.get(i).setCreateTime(parse);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
            int num = sellerMyOrderMapper.getNum(orderCode);
            orderList.get(i).setNum(num);
        }
        return orderList;
    }

    @Override
    public boolean setMap(MyMap map) {
        redisService.redisTemplate.opsForList().rightPush("map",map);
        return true;
    }

    @Override
    public MyMap getMap() {
        List<Object> map = redisService.getCacheList("map");
        MyMap myMap = null;
        for (int i = 0; i < map.size(); i++) {
            MyMap myMap1 = (MyMap) map.get(i);
            if (myMap1.getIndex() == 1) {
                myMap = myMap1;
                myMap1.setIndex(0);
                if (i == (map.size()-1)) {
                    MyMap myMap2 = (MyMap) map.get(0);
                    myMap2.setIndex(1);
                } else {
                    MyMap myMap2 = (MyMap) map.get(i + 1);
                    myMap2.setIndex(1);
                }
                break;
            }
        }
        redisService.deleteObject("map");
        redisService.setCacheList("map",map);
        return myMap;
    }

    /**
     * @Description: 更改仓库状态
     * @Param:
     * @Author: huang
     * @Date: 2023/8/18
     */
    @Override
    public void changeWarehouseStatus(BmWarehouse bmWarehouse) {
        String warehouseCode = bmWarehouse.getWarehouseCode();
        sellerMyOrderMapper.updateWarehouseStatus(bmWarehouse);
        if (bmWarehouse.getWarehouseUserStatus() == 0) {
            List<Object> list = redisService.getCacheList("warehouseAnjian:" + warehouseCode);
            redisService.deleteObject("warehouseAnjian:" + warehouseCode);
            for (int i = 0; i < list.size(); i++) {
                list.set(i,0);
            }
            redisService.setCacheList("warehouseAnjian:" + warehouseCode,list);
        }
        if (bmWarehouse.getWarehouseUserStatus() == 1) {
            redisService.setCacheObject("warehouseShunxu:" + warehouseCode, 0);
        }
    }

    /**
    * @Description: 更改物流人员状态
    * @Param:
    * @return:
    * @Author: huang
    * @Date: 2023/8/18
    */
    @Override
    public void changeWuliuStatus(BmWarehouse bmWarehouse) {
        String warehouseCode = bmWarehouse.getWarehouseCode();
        sellerMyOrderMapper.updateWuliuStatus(bmWarehouse);
        if (bmWarehouse.getWuliuUserStatus() == 0) {
            List<Object> list = redisService.getCacheList("wuliuAnjian:" + warehouseCode);
            redisService.deleteObject("wuliuAnjian:" + warehouseCode);
            for (int i = 0; i < list.size(); i++) {
                list.set(i,0);
            }
            redisService.setCacheList("wuliuAnjian:" + warehouseCode,list);
        }
        if (bmWarehouse.getWarehouseUserStatus() == 1) {
            redisService.setCacheObject("wuliuShunxu:" + warehouseCode, 0);
        }
    }

    @Override
    public BmInorderlist getOrderDetail(String orderCode) {
        BmInorderlist bmInorderlist = bmInorderlistService.selectAll(orderCode);
        List<BmInordergoodslist> bmInordergoodslists = bmInordergoodslistService.selectByOrderCode(orderCode);
        bmInorderlist.setBmInordergoodslists(bmInordergoodslists);
        return bmInorderlist;
    }
}
